package com.qiuzhiyi.activity;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.li.widget.TopNavigationBar;
import com.qiuzhiyi.R;
import com.qiuzhiyi.adapter.FeeHistoryAdapter;
import com.qiuzhiyi.base.QZYBaseActivity;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.item.FeeHistoryItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class FeeHistoryActivity extends QZYBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler_fee)
    RecyclerView mRecyclerFee;

    private FeeHistoryAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fee_history;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        mAdapter = new FeeHistoryAdapter(R.layout.item_fee_history);
        mRecyclerFee.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerFee.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        mAdapter.bindToRecyclerView(mRecyclerFee);
        mAdapter.setEmptyView(R.layout.layout_empty);
        getFeeHistory(getIntent().getStringExtra("id"));
    }

    private void getFeeHistory(String cId) {
        Map<String, String> map = new HashMap<>();
        map.put("cId", cId);
        ObservableHelper.createObservable(RetrofitHelper.createService().getFeeHistory(map))
                .subscribe(new SelectorObserver<BaseItem<List<FeeHistoryItem>>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(BaseItem<List<FeeHistoryItem>> item) {
                        mAdapter.setNewData(item.getObjValue());
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        showLoadingDialog();
                    }
                });

    }
}
